package com.maixy.dailydemo.export;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName ExportTest
 * @Description TODO
 * @Date 2021/8/5 17:29
 * @Author NINGMEI
 */
@Slf4j
public class ExportTest {
    public static void main(String[] args) {
        getExportActivity();
    }

    public static void getExportActivity() {
        try {
            Activity activity1 = new Activity("1","2","3","4","5");
            List<Activity> activityList = Lists.newArrayList();
            activityList.add(activity1);
            activityList.add(activity1);
            activityList.add(activity1);
            activityList.add(activity1);
            activityList.add(activity1);
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个Excel表单,参数为sheet的名字
            for (int i = 0; i < 2; i++) {
                HSSFSheet sheet = workbook.createSheet("课程信息"+ i);
                //创建表头
                setTitle(workbook, sheet, 0, 1);

                //新增数据行，并且设置单元格数据
                int rowNum = 1;
                for (Activity activity : activityList) {
                    HSSFRow row = sheet.createRow(rowNum);
                    row.createCell(0).setCellValue(activity.getValue1());
                    row.createCell(1).setCellValue(activity.getValue2());
                    row.createCell(2).setCellValue(activity.getValue3());
                    row.createCell(3).setCellValue(activity.getValue4());
                    row.createCell(4).setCellValue(activity.getValue5());
                    if (0 == rowNum % 2) {
                        setTitle(workbook, sheet, rowNum, rowNum + 1);
                        rowNum+=2;
                    } else {
                        rowNum++;
                    }

                }
            }
            /*ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            workbook.write(outputStream);
            workbook.close();
            byte[] bytes = outputStream.toByteArray();*/
            writeAndClose("sss.xlsx", workbook);
        } catch (Exception e) {
            log.error("导出失败", e);
        }
    }
    private static void writeAndClose(String fileName, Workbook wb) {
        File file;
        FileOutputStream stream = null;
        try {
            file = new File("D:\\mexport\\");
            stream = new FileOutputStream(new File(file, fileName));
            wb.write(stream);
            //wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
        }
    }

    /***
     * 设置表头
     * @param workbook
     * @param sheet
     */
    private static void setTitle(HSSFWorkbook workbook, HSSFSheet sheet, int firstRow, int lastRow){
        HSSFRow row = sheet.createRow(0);
        //设置列宽，setColumnWidth的第二个参数要乘以256，这个参数的单位是1/256个字符宽度
        sheet.setColumnWidth(0, 25*256);
        sheet.setColumnWidth(1, 25*256);
        sheet.setColumnWidth(2, 10*256);
        sheet.setColumnWidth(3, 100*256);
        sheet.setColumnWidth(4, 20*256);

        //设置为居中加粗
        HSSFCellStyle style = workbook.createCellStyle();
        HSSFFont font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);

        CellRangeAddress cra=new CellRangeAddress(firstRow, lastRow, 0, 4);
        //在sheet里增加合并单元格
        sheet.addMergedRegion(cra);

        HSSFCell cell;
        cell = row.createCell(0);
        cell.setCellValue("课程编号");
        cell.setCellStyle(style);

//        cell = row.createCell(1);
//        cell.setCellValue("课程名称");
//        cell.setCellStyle(style);
//
//        cell = row.createCell(2);
//        cell.setCellValue("课程类型");
//        cell.setCellStyle(style);
//
//        cell = row.createCell(3);
//        cell.setCellValue("课程描述");
//        cell.setCellStyle(style);
//
//        cell = row.createCell(4);
//        cell.setCellValue("创建时间");
//        cell.setCellStyle(style);
    }
}
